package beans;

import java.util.Collection;

/**
 * 配合Codetable使用的动态分页查询Bean.
 * @author wuzh
 */
public class DynaPagedQuery {

    /**
     * 默认一页显示的记录数.
     */
    private static final int DEFAULT_ITEMS_PERPAGE = 20;

    /**
     * 查询的结果集合.
     */
    @SuppressWarnings("unchecked")
    private Collection queryResults;

    /**
     * 查询结果,总共条目个数.
     */
    private long totalCount = -1;

    /**
     * 分页条件:一页的记录条数.
     */
    private int itemsPerPage = DEFAULT_ITEMS_PERPAGE;

    /**
     * 查询条件:查询起始纪录位置.
     */
    private int pagerOffset;

    /**
     * @return Returns the itemsPerPage.
     */
    public int getItemsPerPage() {
        return itemsPerPage;
    }

    /**
     * @param pItemsPerPage The itemsPerPage to set.
     */
    public void setItemsPerPage(int pItemsPerPage) {
        this.itemsPerPage = pItemsPerPage;
    }

    /**
     * @return Returns the queryResults.
     */
    @SuppressWarnings("unchecked")
    public Collection getQueryResults() {
        return queryResults;
    }

    /**
     * @param pQueryResults The queryResults to set.
     */
    @SuppressWarnings("unchecked")
    public void setQueryResults(Collection pQueryResults) {
        this.queryResults = pQueryResults;
    }

    /**
     * @return Returns the totalCount.
     */
    public long getTotalCount() {
        return totalCount;
    }

    /**
     * @param pTotalCount The totalCount to set.
     */
    public void setTotalCount(long pTotalCount) {
        this.totalCount = pTotalCount;
    }

    /**
     * 检查页码是否是有效.
     */
    public void checkPagerOffset() {

        boolean valid = (this.pagerOffset >= 0 && pagerOffset <= totalCount);
        if (!valid) {
            pagerOffset = 0;
        }
    }

    /**
     * 设置查询纪录的起始位置.
     * @param pPagerOffset The pagerOffset to set.
     */
    public void setPagerOffset(int pPagerOffset) {
        this.pagerOffset = pPagerOffset;
    }

    /**
     * @return Returns the pagerOffset.
     */
    public int getPagerOffset() {
        return pagerOffset;
    }

}
